<template>
  <div>
    <!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/service/record' }">诊断管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加诊断记录</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <!-- 添加数据的表单 -->
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">



      <el-form-item label="患者姓名" prop="user_id" >
        <el-input v-model="ruleForm.user_id" placeholder="输入患者ID，懂得都懂"></el-input>
      </el-form-item>
      <el-form-item label="医生姓名" prop="doctor_id">
        <el-input v-model="ruleForm.doctor_id" placeholder="输入医生ID，懂得都懂"></el-input>
      </el-form-item>
      <el-form-item label="药品" prop="medicine_id">
        <el-input v-model="ruleForm.medicine_id" placeholder="输入药品ID，懂得都懂"></el-input>
      </el-form-item>
      <el-form-item label="诊断描述" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="诊断价格" prop="treat_price">
        <el-input v-model="ruleForm.treat_price"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="warning" @click="gotoList">返回列表</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {

      // 表单
      ruleForm: {
        user_id: '',
        doctor_id:'',
        medicine_id:'',
        description:'',
        treat_price:'',
      },
      // 表单规则
      rules: {
        user_id: [
          {required: true, message: '请输入患者ID', trigger: 'blur'},
          {min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur'}
        ],
        doctor_id: [
          {required: true, message: '请输入医生ID', trigger: 'blur'},
          {min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur'}
        ],
        medicine_id: [
          {required: true, message: '请输入药品ID', trigger: 'blur'},
          {min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入诊断描述', trigger: 'blur'},
        ],
        treat_price: [
          {required: true, message: '请输入诊断价格', trigger: 'blur'},
        ],
      }
    };
  },
  methods: {
    // 返回列表
    gotoList() {
      this.$router.push('./');
    },
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9999/records/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
          this.axios
              .create({'headers': {'Authorization': loginInfo.token}})
              .post(url, formData).then((response) => {
            let jsonResult = response.data;
            console.log(jsonResult);
            if (jsonResult.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加诊断记录成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: jsonResult.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },

}
</script>

<style>
.simple-link {
  color: #409eff;
  text-decoration: underline;
}
</style>